# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request, Spider
from urllib.parse import quote
from scrapyselenium.items import ProductItem
import time

class DangdangSpider(scrapy.Spider):
    name = 'dangdang'
    allowed_domains = ['dangdang.com']
    #start_urls = ['http://www.dangdang.com//'] #http://search.dangdang.com/?key=python&SearchFromTop=1&catalog=&page_index=1

    def start_requests(self):
        for key in self.settings.get('KEYWORDS'):
            for page in range(1,self.settings.get('MAX_PAGE')):
                time.sleep(4)
                url = 'http://search.dangdang.com/?key=' + quote(key) + '&page_index=' + str(page)
                yield Request(url = url, callback = self.parse,dont_filter = True)
                
            

    def parse(self, response):
        res = response.selector.css("ul.bigimg li")
        for vo in res:
            item = ProductItem()
            if vo.css("a img::attr(src)").extract_first() == 'images/model/guan/url_none.png':
                #print(vo.css("a img::attr(data-original)").extract_first())
                item['image'] = vo.css("a img::attr(data-original)").extract_first()
            else:
                item['image'] = vo.css("a img::attr(src)").extract_first()
            item['title'] = vo.css("p.name a::attr(title)").extract_first()
            item['price'] = vo.css("p.price span.search_now_price::text").extract_first().replace('¥','')
            #print(vo.css("p.price span.search_now_price::text").extract_first().replace('¥',''))
            item['author'] = vo.css("p.search_book_author").re_first('<a.*?>(.*?)</a>')
            #print('\n\n' + item['title'] + '\n\n')
            yield item
            
        
        
